﻿//#define debug
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

public partial class Admin_moveto : System.Web.UI.Page
{
    public override string StyleSheetTheme
    {
        get
        {
            return ConfigGlobal.GetGlobal().Theme;
        }
        set
        {
            base.StyleSheetTheme = value;
        }
    }

    string tabname, pcol, showcol, pkcol;
    DataTable tab;
    protected void Page_Load(object sender, EventArgs e)
    {
        string typeTab = Request.QueryString["typeTab"];
        if (string.IsNullOrEmpty(typeTab))
        {
            Response.Write("参数传递错误，请返回重试");
            Response.End();
        }
        if (!Page.IsPostBack)
        {
            //Response.Write(Request.UrlReferrer.ToString());
            ViewState["reffer"] = Request.UrlReferrer.ToString();
            ConfigMenus menus = Server.MapPath("~/configs/menu.xml").XmlToObj<ConfigMenus>();
            
            ConfigMenus thisM = menus.ChildMenus.First(x => x.TableName.ToLower() == typeTab.ToLower());
            if (thisM.AutoChild)
            {
                tabname = thisM.TableName;
                pcol = thisM.TableParentColumnName;
                showcol = thisM.TableShowColumnName;
                pkcol = thisM.TablePK;
                string sql = "select * from " + tabname;
                tab = DBFactory.GetConn().exeTable(sql);
                SetupTreeValue();
            }
            else
            {
                Response.Write("未在配置文件中找到相应的分类列表");
                Response.End();
            }            
        }
    }

    private void SetupTreeValue()
    {
        foreach (DataRow row in tab.Rows)
        {
            if (row[pcol].ToString() == "0")
            {
                TreeNode node = new TreeNode();
                node.Text = row[showcol].ToString();
                node.Value = row[pkcol].ToString();

                SetupTreeValue(node, row[pkcol].ToString());
                this.TreeView1.Nodes.Add(node);
            }
        }
    }
    private void SetupTreeValue(TreeNode n,string pid)
    {
        foreach (DataRow row in tab.Rows)
        {
            if (row[pcol].ToString() == pid)
            {
                TreeNode node = new TreeNode();
                node.Text = row[showcol].ToString();
                node.Value = row[pkcol].ToString();

                SetupTreeValue(node, row[pkcol].ToString());
                n.ChildNodes.Add(node);
            }
        }
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        if (this.TreeView1.CheckedNodes.Count == 0)
        {
            Helper.Alert("请选择","noselect");
            return;
        }
        string ids = Request.QueryString["ids"];
        string tabname = Request.QueryString["tab"];
        string col = Request.QueryString["col"];
        string xml=Request.QueryString["xml"];
        string movetoID = this.TreeView1.CheckedNodes[0].Value;
        ConfigTable cTab = ConfigTable.GetConfigTable(tabname, xml);
        ConfigColumns pkC = cTab[cTab.PkFiledName];
        string sqlin = string.Empty;
        string[] id = ids.Split(',');
        List<string> fixedID = new List<string>();
        foreach (string str in id)
        {
            fixedID.Add(pkC.GetSqlValueString(str));
        }
        string newIDs = fixedID.ToArray().ToSplitTrimString();

        sqlin=newIDs;

        //sql构造不完全
        string sql = "update " + tabname + " set "+col+"=" + cTab[col].GetSqlValueString(movetoID) + " where id in (" + sqlin + ")";
        #if debug
        Response.Write(sql);
        Response.Write(ViewState["reffer"].ToString());
        #else
        DBFactory.GetConn().exe0(sql);
        Response.Redirect(ViewState["reffer"].ToString());
        #endif
    }
}